package com.anjian.bamin.anjiandianzimap.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.aiton.administrator.shane_library.shane.ZjbBaseActivity;
import com.aiton.administrator.shane_library.shane.utils.ACache;
import com.aiton.administrator.shane_library.shane.utils.IsMobileNOorPassword;
import com.aiton.administrator.shane_library.shane.utils.PinyinUtils;
import com.aiton.administrator.shane_library.shane.widget.SingleBtnDialog;
import com.alibaba.fastjson.JSON;
import com.anjian.bamin.anjiandianzimap.R;
import com.anjian.bamin.anjiandianzimap.contant.Constant;
import com.anjian.bamin.anjiandianzimap.model.CompanyInfo;
import com.anjian.bamin.anjiandianzimap.model.CompanyList;
import com.anjian.bamin.anjiandianzimap.utils.MyComparator;
import com.anjian.bamin.anjiandianzimap.widget.ChooseDialog;
import com.blankj.utilcode.utils.RegexUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.qqtheme.framework.picker.AddressPicker;
import cn.qqtheme.framework.util.ConvertUtils;

public class AddCompanyActivity extends ZjbBaseActivity implements View.OnClickListener {

    @Bind(R.id.editText_Corporate)
    EditText mEditTextCorporate;
    @Bind(R.id.editText_phone)
    EditText mEditTextPhone;
    @Bind(R.id.editText_type)
    EditText mEditTextType;
    @Bind(R.id.editText_administrator)
    EditText mEditTextAdministrator;
    @Bind(R.id.editText_administrator_phone)
    EditText mEditTextAdministratorPhone;
    @Bind(R.id.editText_name)
    EditText mEditTextName;
    @Bind(R.id.editText_address_area)
    EditText mEditTextAddressArea;
    @Bind(R.id.editText_address_detail)
    EditText mEditTextAddressDetail;
    @Bind(R.id.editText_chooseAddress)
    EditText mEditTextChooseAddress;
    @Bind(R.id.buttonCommit)
    Button mButtonCommit;
    @Bind(R.id.textView_title)
    TextView mTextViewTitle;
    private String mCompangAdd;
    private double mCompanyLat;
    private double mCompanyLng;
    private CompanyInfo mCompanyInfo;
    private boolean isUpdateInfo =false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_company);
        ButterKnife.bind(this);
        init();
    }
    public static Intent newIntent(Context context,CompanyInfo companyInfo){
        Intent intent = new Intent(context, AddCompanyActivity.class);
        intent.putExtra(Constant.INTENT_KEY.COMPANYINFO,companyInfo);
        return intent;
    }
    @Override
    protected void initSP() {

    }

    @Override
    protected void initIntent() {

        mCompanyInfo = (CompanyInfo) getIntent().getSerializableExtra(Constant.INTENT_KEY.COMPANYINFO);
        if(mCompanyInfo != null){
            updateInfo();
            isUpdateInfo=true;
        }
    }

    /**
     * 修改企业信息时设置
     */
    private void updateInfo() {
        mTextViewTitle.setText("修改企业");
        String companyName = mCompanyInfo.getCompanyName();
        String companyAddress = mCompanyInfo.getCompanyAddress();
        String administrator = mCompanyInfo.getAdministrator();
        String administratorPhone = mCompanyInfo.getAdministratorPhone();
        double companyLng = mCompanyInfo.getCompanyLng();
        double companyLat = mCompanyInfo.getCompanyLat();
        String corporate = mCompanyInfo.getCorporate();
        String corporatePhone = mCompanyInfo.getCorporatePhone();
        mEditTextName.setText(companyName);
        mEditTextCorporate.setText(corporate);
        mEditTextPhone.setText(corporatePhone);
        mEditTextChooseAddress.setText(companyAddress);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void findID() {
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void setListeners() {
        findViewById(R.id.buttonCommit).setOnClickListener(this);
        findViewById(R.id.imgBack).setOnClickListener(this);

    }

    @OnClick({R.id.imgBack, R.id.editText_address_area, R.id.editText_chooseAddress, R.id.buttonCommit, R.id.editText_type})
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.buttonCommit:
                addCompanyInfo();
                break;
            case R.id.imgBack:
                finishTo();
                break;
            case R.id.editText_chooseAddress:
                intent.setClass(AddCompanyActivity.this, ChooseAddressActivity.class);
                startActivityForResultTo(intent, Constant.RequestAndResultCode.CHOOSE_ADDRESS);
                break;
            case R.id.editText_type:
                ChooseDialog chooseDialog = new ChooseDialog(AddCompanyActivity.this, 0, false);
                chooseDialog.show();
                chooseDialog.setOnBtnClickListener(new ChooseDialog.OnBtnClickListener() {
                    @Override
                    public void onCompanyClick(int companyInfo) {
                        Log.e("AddCompanyActivity", "onCompanyClick: --->>" + companyInfo);
                        String str = "";
                        switch (companyInfo) {
                            case 0:
                                str = "一类";
                                break;
                            case 1:
                                str = "二类";
                                break;
                            case 2:
                                str = "三类";
                                break;

                        }
                        mEditTextType.setText(str);
                    }
                });
                break;
            case R.id.editText_address_area:
                showAddressChoose();
                break;
        }

    }

    private void addCompanyInfo() {
        final String name = mEditTextName.getText().toString().trim();
        final String alpha = PinyinUtils.getAlpha(name);
        final String addressArea = mEditTextAddressArea.getText().toString().trim();
        final String addressDetail = mEditTextAddressDetail.getText().toString().trim();
        final String corporate = mEditTextCorporate.getText().toString().trim();
        final String companyPhone = mEditTextPhone.getText().toString().trim();
        final String companyType = mEditTextType.getText().toString().trim();
        final String administrator = mEditTextAdministrator.getText().toString().trim();
        final String administratorPhone = mEditTextAdministratorPhone.getText().toString().trim();
        final String chooseAddress = mEditTextChooseAddress.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            mEditTextName.setError("企业名称不能为空");
        } else if (TextUtils.isEmpty(addressArea)) {
            mEditTextAddressArea.setError("企业地区不能为空");
        } else if (TextUtils.isEmpty(addressDetail)) {
            mEditTextAddressDetail.setError("企业详细地址不能为空");
        } else if (!IsMobileNOorPassword.checkChineseName(corporate)) {
            mEditTextCorporate.setError("法人栏输入错误");
        } else if (!RegexUtils.isTel(companyPhone)) {
            mEditTextPhone.setError("企业联系电话输入错误");
        } else if (TextUtils.isEmpty(companyType)) {
            mEditTextType.setError("企业类型不能为空");
        } else if (!IsMobileNOorPassword.checkChineseName(administrator)) {
            mEditTextAdministrator.setError("安全管理员不能为空");
        } else if (!RegexUtils.isTel(administratorPhone)) {
            mEditTextAdministratorPhone.setError("安全管理员电话不能为空");
        } else if (TextUtils.isEmpty(chooseAddress)) {
            mEditTextChooseAddress.setError("地图地位不能为空");
        } else {
            SingleBtnDialog singleBtnDialog = new SingleBtnDialog(AddCompanyActivity.this, "添加成功", "确认");
            singleBtnDialog.setCancelable(false);
            singleBtnDialog.setClicklistener(new SingleBtnDialog.ClickListenerInterface() {
                @Override
                public void doWhat() {
                    CompanyInfo companyInfo = new CompanyInfo(name, alpha, addressDetail, companyPhone, mCompanyLat, mCompanyLng);
                    ACache aCacheCompanyList = ACache.get(AddCompanyActivity.this, Constant.ACACHE.COMPANY_LIST_ACACHE);
                    CompanyList aCacheCompanyListAsObject = (CompanyList) aCacheCompanyList.getAsObject(Constant.ACACHE.COMPANY_LIST);
                    List<CompanyInfo> companyInfoList = aCacheCompanyListAsObject.getCompanyInfoList();
                    companyInfoList.add(companyInfo);
                    Collections.sort(companyInfoList, new MyComparator());

                    aCacheCompanyList.put(Constant.ACACHE.COMPANY_LIST, new CompanyList(companyInfoList));
                    finishTo();
                }
            });
            singleBtnDialog.show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case Constant.RequestAndResultCode.CHOOSE_ADDRESS:
                switch (resultCode) {
                    case Constant.RequestAndResultCode.CHOOSE_ADDRESS:
                        mCompangAdd = data.getStringExtra("公司地址");
                        mCompanyLat = data.getDoubleExtra("公司经度", 0.0);
                        mCompanyLng = data.getDoubleExtra("公司纬度", 0.0);
                        mEditTextChooseAddress.setText(mCompangAdd);
                        break;
                }
                break;
        }
    }

    /**
     * 地址选择器
     */
    private void showAddressChoose() {
        try {
            ArrayList<AddressPicker.Province> data = new ArrayList<AddressPicker.Province>();
            String json = ConvertUtils.toString(getAssets().open("city.json"));
            data.addAll(JSON.parseArray(json, AddressPicker.Province.class));
            AddressPicker picker = new AddressPicker(this, data);
            picker.setSelectedItem("福建", "厦门", "思明");
            picker.setOnAddressPickListener(new AddressPicker.OnAddressPickListener() {
                @Override
                public void onAddressPicked(AddressPicker.Province province, AddressPicker.City city, AddressPicker.County county) {
                    if (county == null) {
                    } else {

                        String mBasicAddress = province.getAreaName() + city.getAreaName() + county.getAreaName();
                        mEditTextAddressArea.setText(mBasicAddress);
                    }
                }
            });
            picker.show();
        } catch (Exception e) {
            Toast.makeText(this, "出错了", Toast.LENGTH_SHORT).show();
        }
    }
}
